package com.cgsrp.cgsrp_shopping.service.impl;

import com.cgsrp.cgsrp_community.domain.Blog;
import com.cgsrp.cgsrp_shopping.dao.ProductPhotoMapper;
import com.cgsrp.cgsrp_shopping.domain.ProductPhoto;
import com.cgsrp.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_shopping.dao.ProductMapper;
import com.cgsrp.cgsrp_shopping.domain.Product;
import com.cgsrp.cgsrp_shopping.service.ProductService;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService{

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductPhotoMapper productPhotoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Product record) {
        return productMapper.insert(record);
    }

    @Override
    public int insertSelective(Product record) {
        return productMapper.insertSelective(record);
    }

    @Override
    public Product selectByPrimaryKey(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Product record) {
        return productMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Product record) {
        return productMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result list(int userId, Integer curPage, Integer pageSize, String searchKey) {
        List<Product> list = productMapper.list(userId,searchKey);
        return getResult(curPage, pageSize, list);
    }

    @Override
    public Result add(Product product) {
        int i = productMapper.insert(product);
        for (String photo : product.getPhotos()) {
            ProductPhoto productPhoto = new ProductPhoto();
            productPhoto.setProductId(product.getId());
            productPhoto.setPhotopath(photo);
            productPhotoMapper.insert(productPhoto);
        }
        return i>0?Result.success("上传成功"):Result.failure("上传失败");
    }

    @Override
    public Result del(Integer productId) {
        return productMapper.del(productId)>0?Result.success("下架成功"):Result.failure("下架失败");
    }

    @Override
    public Result up(Integer productId) {
        return productMapper.up(productId)>0?Result.success("上架成功"):Result.failure("上架失败");
    }

    @Override
    public Result update(Product product, Integer userId) {
        int i = productMapper.updateByPrimaryKeySelective(product);
        productPhotoMapper.delete(product.getId());
        for (String photo : product.getPhotos()) {
            ProductPhoto productPhoto = new ProductPhoto();
            productPhoto.setProductId(product.getId());
            productPhoto.setPhotopath(photo);
            productPhotoMapper.insert(productPhoto);
        }
        return i>0?Result.success("更新成功"):Result.failure("更新失败");
    }

    @Override
    public Result buyList(Integer curPage, Integer pageSize, String searchKey) {
        List<Product> list = productMapper.buyList(searchKey);
        return getResult(curPage, pageSize, list);
    }

    public Result getResult(Integer curPage, Integer pageSize, List<Product> list) {
        List<Product> result;
        if (pageSize<list.size()-(curPage-1)*pageSize){
            result= list.subList((curPage - 1) * pageSize, (curPage - 1) * pageSize + pageSize);
        }else {
            result=list.subList((curPage - 1) * pageSize, list.size());
        }
        PageInfo<Product> pageInfo = new PageInfo<>(result);
        pageInfo.setTotal(list.size());
        PageHelper.startPage(curPage, pageSize);
        return Result.suc("查询成功",pageInfo);
    }
}
